package minh.bac.wcbrazil2014.favorite;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.adapter.FavoriteChildLVAdapter;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.homnay.HomNayFragment;
import minh.bac.wcbrazil2014.homnay.HomNayItemListViewFM;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListViewChildFM extends Fragment{

	ListView listView;
	
	String nameTeam, imgLink;
	private ArrayList<MatchScheduleObj> mArrayListData;
	private MySQLiteManager mMySQLiteManager;
	private FavoriteChildLVAdapter mChildLVAdapter;
	DisplayImageOptions displayImageOptions;
	public static ListViewChildFM newInstance(String nameTeam, String imgLink)
	{
		ListViewChildFM listViewChildFM = new ListViewChildFM();
		//
		Bundle bundle = new Bundle();
		bundle.putString(WCTAG.KEY_NAMETEAM, nameTeam);
		bundle.putString(WCTAG.KEY_IMGLINK_TEAM, imgLink);
		listViewChildFM.setArguments(bundle);
		//
		return listViewChildFM;		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.listview_child_favorite, container, false);
		nameTeam = getArguments().getString(WCTAG.KEY_NAMETEAM);
		listView = (ListView) rootView.findViewById(R.id.favorite_listview_child);
		mArrayListData = new ArrayList<MatchScheduleObj>();
		mMySQLiteManager = new MySQLiteManager(getActivity());
		displayImageOptions = new DisplayImageOptions.Builder()
		.cacheInMemory(false)
		.cacheOnDisc(true)		
		.resetViewBeforeLoading(false)
		.bitmapConfig(Bitmap.Config.RGB_565)										
		.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
		.showImageOnFail(R.drawable.ic_emblem_club_placeholder)		
		.imageScaleType(ImageScaleType.EXACTLY)										
		.build();	
		//
		LoadData();
		//
		mChildLVAdapter = new FavoriteChildLVAdapter(getActivity(), R.layout.favorite_listivew_item, mArrayListData, displayImageOptions);
		//	
		listView.setAdapter(mChildLVAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				FavoriteItemChildLV fragment = FavoriteItemChildLV.newInstance(nameTeam, imgLink);
				MatchScheduleObj matchScheduleObj = mArrayListData.get(position);
				fragment.setMatchScheduleObj(matchScheduleObj);
				FavoriteChildFM.switchFragment(R.id.container_favorite_items, fragment, true, null);
			}
		});
		return rootView;
	}
	public void LoadData()
	{
		ArrayList<VongBangObj> temp  = new ArrayList<VongBangObj>();
		temp = mMySQLiteManager.getVongBangByName(nameTeam);
		if(temp != null )
		{
			mArrayListData.addAll(temp);
		}

		ArrayList<VongDau> temp2  = new ArrayList<VongDau>();
		temp2 = mMySQLiteManager.getVongDauByName(nameTeam);
		if(temp2 != null )
		{
			mArrayListData.addAll(temp2);
		}		
	}
}
